package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.DynamicLikeUsersQueryObject;
import cn.wolfcode.trip.base.query.DynamicQueryObject;
import cn.wolfcode.trip.base.service.IDynamicService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DynamicServiceImpl implements IDynamicService {
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;

    @Override
    public PageInfo<DynamicInfo> query(DynamicQueryObject qo) {
        //获得该用户关注的人的id集合
        List<Long> focusList = userMapper.queryFocusIdList(qo);
        //将自己的集合加到id集合中
        Long currentUserId = UserContext.getCurrentUser().getId();
        focusList.add(currentUserId);
        //给查询对象设置关注的人的id集合
        qo.setFocusList(focusList);
        //设置分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        //查询动态集合
        List<Dynamic> list = dynamicMapper.queryForList(qo);
        //获取pages数据
        PageInfo<Dynamic> dynamicPageInfo = new PageInfo<>(list);
//        System.out.println(dynamicPageInfo.getPages());

        //新建一个动态信息的集合
        List<DynamicInfo> dynamicInfos = new ArrayList<>();
        //遍历动态集合,将每个动态的评论集,和点赞集查询出来
        for (Dynamic dynamic : list) {
            //新建一个动态信息对象
            DynamicInfo dynamicInfo = new DynamicInfo();
            //将动态设置到动态信息对象中
            dynamicInfo.setDynamic(dynamic);
            //查询点赞人的集合
            //新建查询对象,不能使用分页
            DynamicLikeUsersQueryObject dqo = new DynamicLikeUsersQueryObject();
            //设置查询对象的信息
            dqo.setDynamicId(dynamic.getId());
            dqo.setFocusList(focusList);
            //进行点赞用户对象的查询
            List<User> likeUsers = userMapper.selectForLikeDynamicUsers(dqo);
            //设置点过赞的用户的集合
            dynamicInfo.setLikeUsers(likeUsers);
            //查询该条动态的评论集
            List<DynamicComment> comments = dynamicCommentMapper.selectDynamicComment(dqo);
            //将评论集合设置到动态信息中
            dynamicInfo.setComments(comments);
            //将该动态信息对象放到集合中
            dynamicInfos.add(dynamicInfo);
        }
        PageInfo<DynamicInfo> dynamicInfoPageInfo = new PageInfo<>(dynamicInfos);
        dynamicInfoPageInfo.setPages(dynamicPageInfo.getPages());
        return dynamicInfoPageInfo;
    }

    @Override
    public void save(Dynamic dynamic) {
        dynamic.setUser(UserContext.getCurrentUser());
        dynamicMapper.insert(dynamic);

    }

    /**
     * 查询是否已点赞朋友圈
     * @param dynamicId 动态id
     */
    @Override
    public void like(Long dynamicId) {
        //查询当前用户的id
        Long userId = UserContext.getCurrentUser().getId();
        int num = userMapper.queryDynamicIsLike(userId, dynamicId);
        if (num==0){
            userMapper.likeDynamic(userId, dynamicId);
        }else {
            userMapper.cancelLikeDynamic(userId, dynamicId);
        }
    }

}
